package controller;

import org.example.User;
import org.example.VO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

//@Controller
public class UserController {

    //上传文件
    @RequestMapping("/quick7")
    @ResponseBody//不进行页面跳转
    //uploadFile要和表单中的文件名称一致
    public void save7(String username , MultipartFile uploadFile){
        System.out.println(username);
        //保存文件
        //获取文件名称
        String name = uploadFile.getOriginalFilename();
        //转移到磁盘是
        try {
            uploadFile.transferTo(new File("D:\\迅雷下载\\"+name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取请求头
    @RequestMapping("/quick6")
    @ResponseBody//不进行页面跳转
    public void save6(@RequestHeader(value = "User-Agent",required = false) String username){
        System.out.println(username);
    }

    @RequestMapping("/quick5/{username}")
    @ResponseBody//不进行页面跳转
    public void save5(@PathVariable("username") String username){
        System.out.println(username);
    }
    @RequestMapping("/quick4")
    @ResponseBody//不进行页面跳转
    public void save4(VO vo){

    }
    //获得请求参数
    @RequestMapping("/quick3")
    @ResponseBody//不进行页面跳转
    public void save3(String username,int age){
        System.out.println(username);
        System.out.println(age);
    }
    //返回对象或集合
    @RequestMapping("/quick2")
    @ResponseBody//回写数据，直接返回字符串
    public User save2(){
        User user=new User();
        user.setName("zhangsan");
        user.setAge(25);
        return user;
    }
    //请求映射
    @RequestMapping("/quick")
    public String save(){
        System.out.println("Controller save is running");
        return "success.jsp";
    }
}
